package com.hiq.psycho;

import java.io.*;
import java.util.*;

public class Interlocutor {

	String paramFile0 = "data\\eng\\interlocutor.txt"; 
	String paramFile1 = "data\\rus\\interlocutor.txt"; 
	
	int mode = 0;
	
	int currentPar;
	
	int total;
	int filled = 0;
	
	List<String> parameters = new ArrayList<String>();
	Map<String, List<String>> questions = new Hashtable<String, List<String>>();
	Map<String, String> info = new Hashtable<String, String>();
	
	
	public Interlocutor(int i) {
		mode = i;
		
		readParams();
		total = parameters.size();
	}
	
	public int getTotal(){
		return this.total;
	}
	
	public int getFilled(){
		return this.filled;
	}
	
	
	private void readParams(){
		try {
			String f;
			if (mode == 0) f = this.paramFile0; else f = this.paramFile1;
			BufferedReader br = new BufferedReader(new FileReader(f));
			String line;
			while((line = br.readLine()) != null){
				StringTokenizer st = new StringTokenizer(line, "#");
				String str1 = st.nextToken();
				String str2 = st.nextToken();
				st = new StringTokenizer(str2, "/");
				List<String> list = new ArrayList<String>();
				while(st.hasMoreElements()){
					list.add(st.nextToken());
				}
				parameters.add(str1);
				questions.put(str1, list);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getQuestion(){
		String res = "Hmph";
		for (String p: this.parameters){
			String a = info.get(p);
			if (a==null){
				List<String> list = questions.get(p);
				int n = list.size();
				int i = (int) Math.floor(Math.random() * n);
				res = list.get(i);
				currentPar = i;
				break;
			}
		}		
		
		return res;
	}
	
	public void setParameter(String answer){
		String par = parameters.get(currentPar);
		info.put(par, answer);
		filled++;
	}
	
	
}
